package chabernac.android.wordbattle.paint.crosstile;

import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.paint.SVGFactory;

public class CrossTileBitmapFactory {
	
	public static final String UpLeftCoordinate = "a1";
	public static final String UpRightCoordinate = "a1";
	public static final String DownLeftCoordinate = "a1";
	public static final String DownRightCoordinate = "a1";
	
	private static CrossTileBitmapFactory myInstance = null;
	
	private Map<String, Bitmap> myBitmapCache = null;
	
	protected CrossTileBitmapFactory() {
		myBitmapCache = new HashMap<String, Bitmap>();
	}
	
	public static CrossTileBitmapFactory getInstance()
	{
		if (myInstance == null)
		{
			myInstance = new CrossTileBitmapFactory();
		}
		return myInstance;
	}
	
	public Bitmap getCrossTileBitmap(
			String aResourceName, 
			WordBattleView aWordBattleView)
	{
		Bitmap aResultBitmap = null;
		// Check whether the type is already created
		if (myBitmapCache.containsKey(aResourceName) == true)
		{
			aResultBitmap = myBitmapCache.get(aResourceName);
		}
		else
		{
			float aColumnWidth = aWordBattleView.getWidthPerColumn();
			float aRowHeight = aWordBattleView.getHeightPerRow();
			aResultBitmap = getSvgBitmap(aResourceName, (int)aColumnWidth, (int)aRowHeight);
			// Add the result bitmap to the cache
			myBitmapCache.put(aResourceName, aResultBitmap);
		}
		return aResultBitmap;
	}
	
	private Bitmap getSvgBitmap(String aResourceName, int aWidth, int aHeight){
		 Bitmap aBitmap = Bitmap.createBitmap(aWidth, aHeight, Bitmap.Config.ARGB_8888);
		 Canvas aCanvas = new Canvas(aBitmap); 
		 Picture aPicture = SVGFactory.createSVG(aResourceName);
		 aCanvas.drawPicture(aPicture, new Rect(0,0,aWidth,aHeight)); 
		 return aBitmap;
	}
}
